package net.zhengxinyun.performance.data;

import com.alibaba.fastjson.JSONArray;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.Util.WorkbookUtil;
import net.zhengxinyun.performance.entity.SixScoreEntity;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.filter.SixTownFilter;
import net.zhengxinyun.performance.service.indicators.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hyp1202 on 2019/2/21.
 */
@Component
public class SixScoreData {

    private SixIndicatorsService sixIndicatorsService;
    private SixScoreService sixScoreService;
    private SixTownService sixTownService;

    @Autowired
    public SixScoreData(SixIndicatorsService sixIndicatorsService, SixScoreService sixScoreService, SixTownService sixTownService) {
        this.sixIndicatorsService = sixIndicatorsService;
        this.sixScoreService = sixScoreService;
        this.sixTownService = sixTownService;
    }

    private static final String COL_INDEX_NAME = "二级指标名";
    private static final String COL_YEAR = "年份";
    private static final String COL_MONTH = "月份";
    private static final String COL_DONGYANG = "东洋乡";
    private static final String COL_ZHANGCHENG = "樟城镇";
    private static final String COL_DAYANGX = "大洋乡";
    private static final String COL_CHIXI = "赤锡乡";
    private static final String COL_HONGXING = "红星镇";
    private static final String COL_QINGLIANG = "清凉镇";
    private static final String COL_SONGKOU = "嵩口镇";
    private static final String COL_DANYUN = "丹云乡";
    private static final String COL_GAIYANG = "盖洋乡";
    private static final String COL_XIABO = "霞拔乡";
    private static final String COL_WUTONG = "梧桐镇";
    private static final String COL_GELING = "葛岭镇";
    private static final String COL_BAIYUN = "白云乡";
    private static final String COL_FUKOU = "洑口乡";
    private static final String COL_TONGAN = "同安镇";
    private static final String COL_PANGU = "盘谷乡";
    private static final String COL_CHENGFENG = "城峰镇";
    private static final String COL_LINGLU = "岭路乡";
    private static final String COL_TANGQIAN = "塘前乡";
    private static final String COL_FUQUAN = "福泉镇";
    private static final String COL_CHANGQING = "长庆镇";


    private static final String[] LABLE_ZERO = {"二级指标名","年份","月份","东洋乡","樟城镇","大洋乡","赤锡乡","红星镇","清凉镇","嵩口镇","丹云乡",
            "盖洋乡","霞拔乡","梧桐镇","葛岭镇","白云乡","洑口乡","同安镇","盘谷乡","城峰镇","岭路乡","塘前乡","福泉镇","长庆镇"};

    public List<List<SixScoreEntity>> importTownScore(String filePath) throws Exception{
        List<List<SixScoreEntity>> list = new ArrayList<>();
        List<String> errors = new ArrayList<>();
        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> iterator = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lableListZero = new ArrayList<>();
        while (iterator.hasNext()){
            String str = iterator.next().getStringCellValue();
            if (!"".equals(str)){
                lableListZero.add(str);
                if (length>25){
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }
        if (!Util.arrayEquals(lableListZero.toArray(),LABLE_ZERO)){
            throw new Exception("请选择正确模板上传");
        }
        for (int rowIdx=1;rowIdx <= sheet.getLastRowNum();rowIdx++){
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)){
                continue;
            }
            try {
                List<SixScoreEntity> townScoreFilterList = new ArrayList<>();
                SixIndicatorsFilter townIndicatorsFilter = new SixIndicatorsFilter();
                String indexName = WorkbookUtil.getStringCellValue(row,COL_INDEX_NAME);
                if (StringUtils.isBlank(indexName)){
                    throw  new Exception("指标名不能为空");
                }
                else {
                    townIndicatorsFilter.setIndexName(indexName);
                }
                String year = WorkbookUtil.getStringCellValue(row,COL_YEAR);
                if (StringUtils.isBlank(year)){
                    throw new Exception("年份不能为空");
                }else {
                    townIndicatorsFilter.setDateTime(year);
                    townIndicatorsFilter = sixIndicatorsService.query(townIndicatorsFilter).get(0);
                }
                String month = WorkbookUtil.getStringCellValue(row,COL_MONTH);
                if (StringUtils.isBlank(month)){
                    throw new Exception("月份不能为空");
                }else {
                    if (month.length() == 1){
                        month = "0"+month;
                    }
                }
                String dongyang = WorkbookUtil.getStringCellValue(row,COL_DONGYANG);
                if (StringUtils.isNotBlank(dongyang)){
                    addList(townScoreFilterList,COL_DONGYANG,townIndicatorsFilter,dongyang,month);
                }
                String zhangcheng = WorkbookUtil.getStringCellValue(row,COL_ZHANGCHENG);
                if(StringUtils.isNotBlank(zhangcheng)){
                    addList(townScoreFilterList,COL_ZHANGCHENG,townIndicatorsFilter,zhangcheng,month);
                }
                String dayang = WorkbookUtil.getStringCellValue(row,COL_DAYANGX);
                if (StringUtils.isNotBlank(dayang)){
                    addList(townScoreFilterList,COL_DAYANGX,townIndicatorsFilter,dayang,month);
                }
                String chixi = WorkbookUtil.getStringCellValue(row,COL_CHIXI);
                if (StringUtils.isNotBlank(chixi)){
                    addList(townScoreFilterList,COL_CHIXI,townIndicatorsFilter,chixi,month);
                }
                String hongxing = WorkbookUtil.getStringCellValue(row,COL_HONGXING);
                if (StringUtils.isNotBlank(hongxing)){
                    addList(townScoreFilterList,COL_HONGXING,townIndicatorsFilter,hongxing,month);
                }
                String qingliang = WorkbookUtil.getStringCellValue(row,COL_QINGLIANG);
                if (StringUtils.isNotBlank(qingliang)){
                    addList(townScoreFilterList,COL_QINGLIANG,townIndicatorsFilter,qingliang,month);
                }
                String songkou = WorkbookUtil.getStringCellValue(row,COL_SONGKOU);
                if (StringUtils.isNotBlank(songkou)){
                    addList(townScoreFilterList,COL_SONGKOU,townIndicatorsFilter,songkou,month);
                }
                String danyun = WorkbookUtil.getStringCellValue(row,COL_DANYUN);
                if (StringUtils.isNotBlank(danyun)){
                    addList(townScoreFilterList,COL_DANYUN,townIndicatorsFilter,danyun,month);
                }
                String gaiyang = WorkbookUtil.getStringCellValue(row,COL_GAIYANG);
                if (StringUtils.isNotBlank(gaiyang)){
                    addList(townScoreFilterList,COL_GAIYANG,townIndicatorsFilter,gaiyang,month);
                }
                String xiabo = WorkbookUtil.getStringCellValue(row,COL_XIABO);
                if (StringUtils.isNotBlank(xiabo)){
                    addList(townScoreFilterList,COL_XIABO,townIndicatorsFilter,xiabo,month);
                }
                String wutong = WorkbookUtil.getStringCellValue(row,COL_WUTONG);
                if (StringUtils.isNotBlank(wutong)){
                    addList(townScoreFilterList,COL_WUTONG,townIndicatorsFilter,wutong,month);
                }
                String geling = WorkbookUtil.getStringCellValue(row,COL_GELING);
                if (StringUtils.isNotBlank(geling)){
                    addList(townScoreFilterList,COL_GELING,townIndicatorsFilter,geling,month);
                }
                String baiyun = WorkbookUtil.getStringCellValue(row,COL_BAIYUN);
                if (StringUtils.isNotBlank(baiyun)){
                    addList(townScoreFilterList,COL_BAIYUN,townIndicatorsFilter,baiyun,month);
                }
                String fukou = WorkbookUtil.getStringCellValue(row,COL_FUKOU);
                if (StringUtils.isNotBlank(fukou)){
                    addList(townScoreFilterList,COL_FUKOU,townIndicatorsFilter,fukou,month);
                }
                String tongan = WorkbookUtil.getStringCellValue(row,COL_TONGAN);
                if (StringUtils.isNotBlank(tongan)){
                    addList(townScoreFilterList,COL_TONGAN,townIndicatorsFilter,tongan,month);
                }
                String pangu = WorkbookUtil.getStringCellValue(row,COL_PANGU);
                if (StringUtils.isNotBlank(pangu)){
                    addList(townScoreFilterList,COL_PANGU,townIndicatorsFilter,pangu,month);
                }
                String chengfeng = WorkbookUtil.getStringCellValue(row,COL_CHENGFENG);
                if (StringUtils.isNotBlank(chengfeng)){
                    addList(townScoreFilterList,COL_CHENGFENG,townIndicatorsFilter,chengfeng,month);
                }
                String linglu = WorkbookUtil.getStringCellValue(row,COL_LINGLU);
                if (StringUtils.isNotBlank(linglu)){
                    addList(townScoreFilterList,COL_LINGLU,townIndicatorsFilter,linglu,month);
                }
                String tangqian = WorkbookUtil.getStringCellValue(row,COL_LINGLU);
                if (StringUtils.isNotBlank(tangqian)){
                    addList(townScoreFilterList,COL_TANGQIAN,townIndicatorsFilter,tangqian,month);
                }
                String fuquan = WorkbookUtil.getStringCellValue(row,COL_FUQUAN);
                if (StringUtils.isNotBlank(fuquan)){
                    addList(townScoreFilterList,COL_FUQUAN,townIndicatorsFilter,fuquan,month);
                }
                String changqing = WorkbookUtil.getStringCellValue(row,COL_CHANGQING);
                if (StringUtils.isNotBlank(changqing)){
                    addList(townScoreFilterList,COL_CHANGQING,townIndicatorsFilter,changqing,month);
                }
                list.add(townScoreFilterList);
            }catch (Exception e){
                errors.add("第" + rowIdx + "行：" + e.getMessage());
            }
        }
        if (errors.isEmpty())
            throw new Exception(JSONArray.toJSONString(errors));
        return list;
    }

    public  List<SixScoreEntity> addList(List<SixScoreEntity> list,String townName,SixIndicatorsFilter sixIndicatorsFilter,String score,String month){
        SixTownFilter sixTownFilter = new SixTownFilter();
        sixTownFilter.setIndexId(sixIndicatorsFilter.getId().toString());
        sixTownFilter.setTownName(townName);
        sixTownFilter = sixTownService.query(sixTownFilter).get(0);
        SixScoreEntity townScoreEntity = new SixScoreEntity(score,sixIndicatorsFilter.getId().toString(),sixTownFilter.getId().toString(),month);
        townScoreEntity.setAudit("0");
        list.add(townScoreEntity);
        return list;
    }
}
